<template>
  <div>
    <el-form
      :model="formData"
      label-width="90px"
      :rules="FormRules"
      ref="addForm"
      v-loading="loading"
    >
      <el-form-item label="账号" prop="account" v-if="isType != 2">
        <el-input v-model="formData.account" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="isType != 1">
        <el-input v-model="formData.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <!-- <el-form-item label="员工" v-if="isType != 2">
        <el-select v-model="formData.pkPsndoc" placeholder="请选择">
          <el-option label value></el-option>
          <el-option
            v-for="item in options"
            :key="item.pkPsndoc"
            :label="item.psnname"
            :value="item.pkPsndoc"
          ></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="岗位" v-if="isType != 2">
        <el-select v-model="formData.pkOmJob" placeholder="请选择">
          <el-option
            v-for="item in station_list"
            :key="item.pkOmJob"
            :label="item.jobname"
            :value="item.pkOmJob+','+item.jobname"
          ></el-option>
          <!-- <el-option
            v-for="item in station_list"
            :key="item.pkOmJob"
            :label="item.jobname"
            :value="item.pkOmJob"
          ></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="账号" v-if="isType != 2">
        <el-select v-model="formData.cuserid" placeholder="请选择" filterable>
          <el-option
            v-for="item in user_list"
            :key="item.cuserid"
            :label="item.userName"
            :value="item.cuserid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handSave()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  userAdd,
  userEdit,
  userChangePwd,
  getRoleList,
  smUserList,
  omJobList,
  omJobListCuserJob
} from "../../../api/baseSetting";
export default {
  data() {
    return {
      formData: {
        account: "",
        password: "",
        pkOmJob: "",
        cuserid: ""
      },
      FormRules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        pkOmJob: [{ required: true, message: "请选择岗位", trigger: "blur" }],
        cuserid: [{ required: true, message: "请选择账号", trigger: "blur" }]
      },
      isType: 0, //1编辑 2修改密码
      user_list: [], //账号列表
      station_list: [], //岗位列表
      loading: true
    };
  },
  props: {
    sendFormData: {
      num: {
        type: Object
      }
    }
  },
  methods: {
    //点击保存
    handSave() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          var formData = this.formData;
          let pkOmJob = formData.pkOmJob.split(',')[0];
          let jobname = formData.pkOmJob.split(',')[1];
          formData.pkOmJob = pkOmJob;
          formData.jobname = jobname;
          this.loading = true;
          if (this.sendFormData.userId) {
            //编辑
            if (this.isType == 1) {
              userEdit(formData).then(res => {
                this.$message({
                  message: "成功",
                  type: "success"
                });
                this.$emit("hideTotas");
              });
            }
            //修改密码
            if (this.isType == 2) {
              let sendDta = {
                password: formData.password,
                userId: formData.userId
              };
              userChangePwd(sendDta).then(res => {
                this.$message({
                  message: "成功",
                  type: "success"
                });
                this.$emit("hideTotas");
              });
            }
          } else {
            //新增
            userAdd(formData).then(res => {
              this.$message({
                message: "成功",
                type: "success"
              });
              this.$emit("hideTotas");
            });
          }
        } else {
          this.$message({
            message: "请完善信息",
            type: "warning"
          });
        }
      });
    },
    //获取账号列表
    UserList() {
      smUserList({
        page: {
          page: 1,
          pageSize: 100000
        }
      }).then(res => {
        this.user_list = res.records;
        this.loading = false;
      });
    },
    //岗位列表
    stationList() {
      omJobList({
        page: {
          page: 1,
          pageSize: 100000
        }
      }).then(res => {
        this.station_list = res.records;
      });
    }
  },
  created() {
    if (this.sendFormData.isType == 2) {
      this.loading = false;
    }else{
      this.UserList();
      this.stationList();
    }
  },
  mounted() {
    this.isType = this.sendFormData.isType;
    if (this.sendFormData.userId) {
      var data = this.sendFormData;
      this.formData = this.sendFormData;
      this.formData.pkOmJob = this.sendFormData.pkOmJob? this.sendFormData.pkOmJob+','+this.sendFormData.jobname:'';
      // this.formData = {
      //   userId: data.userId,
      //   account: data.account,
      //   // password: "",
      //   pkOmJob: data.pkOmJob,
      // };
    }
  }
};
</script>

<style lang="less" scoped>
</style>
